package drawing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import malaria_acd.AddNewNameActivity;

import project.malaria.malaria_acd.R;
import project.malaria.malaria_acd.R.id;
import project.malaria.malaria_acd.R.layout;

import util.DrawView;
import util.MessageUtil;
import util.SessionUtil;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Bitmap.*;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class DrawNameActivity extends Activity {
		DrawView drawView,drawView2;
		DrawView mView;
	private Boolean isPencil,isErasor,isClear;
	private Button saveimgs;
	private ImageView back;
	private RelativeLayout fname,lname;
	private File root;
	private Bitmap b_fname,b_lname;
	private View v;
	
	/***Variable:: SharedPreference***/
	public static final String PREFS_NAME = "DrawingFName";
	public static  String shDrawFName;
	private  SharedPreferences share1;
	
	public static final String PREFS_NAME2 = "DrawingLName";
	public static  String shDrawLname;
	private  SharedPreferences share2;
	
	/***Variable:: Session***/
	public String trigger;
	private String ACD_ID;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drawname);
		initializeValue();
		getSession();

		drawView = new DrawView(this);
		fname.addView(drawView);
		fname.setDrawingCacheEnabled(true);
		fname.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		fname.layout(0, 0, fname.getMeasuredWidth(), fname.getMeasuredHeight()); 
		fname.buildDrawingCache(true);
		fname.setDrawingCacheEnabled(false);
		
		drawView2 = new DrawView(this);
		lname.addView(drawView2);
		lname.setDrawingCacheEnabled(true);
		lname.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		lname.layout(0, 0, lname.getMeasuredWidth(), lname.getMeasuredHeight()); 
		lname.buildDrawingCache(true);
		lname.setDrawingCacheEnabled(false);

		saveimgs.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	        	/***First Name***/
	        	SharedPreferences.Editor edit = share1.edit();
	        	View v1 = getWindow().getDecorView().findViewById(R.id.signImageFname);
	      		v1.setDrawingCacheEnabled(true);
	      		b_fname = Bitmap.createBitmap(v1.getDrawingCache());
	      		v1.setDrawingCacheEnabled(false);
	      		String file_fname = SaveImageFName(b_fname,"fn");
    	   	 	edit.putString(shDrawFName,file_fname);
    	   	 	edit.commit();
    	   	 	
	        	/***Lastname***/
    	   	 	SharedPreferences.Editor edit2 = share2.edit();
    	   	 	View v2 = getWindow().getDecorView().findViewById(R.id.signImageLname);
	      		v2.setDrawingCacheEnabled(true);
	      		b_lname = Bitmap.createBitmap(v2.getDrawingCache());
	      		v2.setDrawingCacheEnabled(false);
	        	String file_lname = SaveImageLName(b_lname,"ln");     	
	        	edit2.putString(shDrawLname,file_lname);	
	        	edit2.commit();

	        	sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
	        	
	        		
	        	Intent i = new Intent(DrawNameActivity.this, AddNewNameActivity.class);
	        				SessionUtil.setValue("pathFName",file_fname);
	        				SessionUtil.setValue("pathLName",file_lname);
	        				SessionUtil.setValue("trigger","1");
	        				SessionUtil.setValue("typeName","1");     
	        				finish();
   				startActivityForResult(i,0);	
	        	
	        }
	    });		
		
		back.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				 onBackPressed();
			}
	    });		
	}
	private String SaveImageFName(Bitmap finalBitmap,String filetype) {
	    String root = Environment.getExternalStorageDirectory().toString();
	    File myDir = new File(root + "/ACD/images");    
	    myDir.mkdirs();
	    
	    String fname = filetype + ACD_ID+ ".jpg";
	    File file = new File (myDir, fname);
	    if (file.exists ()) file.delete (); 
	    try {
	           FileOutputStream out = new FileOutputStream(file);
	           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
	           out.flush();
	           out.close(); 
	           

	    } catch (Exception e) {
	           e.printStackTrace();
	           
	    }
	    return fname;
	    
	}
		
	private String SaveImageLName(Bitmap finalBitmap,String filetype) {
	    String root = Environment.getExternalStorageDirectory().toString();
	    File myDir = new File(root + "/ACD/images");    
	    myDir.mkdirs();
	    
	    String fname = filetype + ACD_ID+ ".jpg";	    
	    File file = new File (myDir, fname);
	    if (file.exists ()) file.delete ();
	    try {
	           FileOutputStream out = new FileOutputStream(file);
	           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
	           out.flush();
	           out.close(); 
	    } catch (Exception e) {
	           e.printStackTrace();         
	    }
	    return fname;
	}
	
		public void funcClear(View v) {		
			funcClear();
		}

	
		public void funcClear() {
			drawView.requestFocus();   
			drawView.clear();
		}
		
		public void funcClear2(View v) {		
			funcClear2();
		}
		public void funcClear2() {
			drawView2.requestFocus();   
			drawView2.clear();
		}
		private void initializeValue()
		{
			share1= this.getSharedPreferences(PREFS_NAME, 0);
			share2= this.getSharedPreferences(PREFS_NAME2, 0);
			fname = (RelativeLayout) findViewById(R.id.signImageFname);
			lname = (RelativeLayout) findViewById(R.id.signImageLname);
			saveimgs = (Button) findViewById(R.id.saveimg);
			back = (ImageView) findViewById(R.id.backbutton1);
		}	
		public void getSession()
		{	
			 ACD_ID 		= SessionUtil.getValue("NEWACD_ID");
		}
		
	
		
}
